import { _decorator,UITransformComponent ,SpriteComponent,Label, sp,assetManager,Camera, Node,Color,math, Sprite,SystemEvent, LabelComponent, SkinnedMeshRenderer,Prefab,v3, Vec3,SkeletalAnimation, ProgressBar, tween, UITransform, sys, SpriteFrame } from 'cc';

import BaseView from '../../Script/game/BaseView';
import {Bullet} from '../../Script/config/BaseData';
import Tool from '../../Script/tool/Tool'
// import { luckyBox } from './luckyBox';
import GD from '../../Script/config/GD';
import GDef from '../../Script/config/GDef';
import SdkMgr from '../../Script/tyq3.x/SdkMgr';
import TyqEventMgr from '../../Script/tyq3.x/tyq-event-mgr';
import { tyqSDK } from '../../Script/tyq3.x/tyq-sdk';
const { bits } = math;
const { ccclass, property } = _decorator;

@ccclass('preEnd')
export class preEnd extends BaseView {
    @property({
        type: sp.Skeleton
    })
    comSpineTitle: sp.Skeleton = null;
    @property({
        type: Node
    })
    nodeProgress: Node = null;
    @property({
        type: sp.Skeleton
    })
    comSpineLight: sp.Skeleton = null;
    @property({
        type: Node
    })
    nodeGan: Node = null;
    @property({
        type: Node
    })
    nodeBottom: Node = null;
    @property({
        type: Node
    })
    btnGiveup: Node = null;
    @property({
        type: Node
    })
    btnPlay: Node = null;
    @property({
        type: SpriteComponent
    })
    sprSprite: SpriteComponent[] = [];
    @property({
        type: Node
    })
    nodeBone: Node = null;
    @property({
        type: Label
    })
    labelNumSrc: Label = null;
    @property({
        type: Label
    })
    labelNum: Label = null;
    @property({
        type: SpriteFrame
    })
    sprBtn: SpriteFrame = null;

    private nodeManghe: Node = 2;
    private speed: number = 6;
    private type: number = 0;
    private coin: number = 0;
    private intCoinRatio: number = 2;
    private intClickCount: number = 0;

    onLoad() {
        let self = this;
        this.on(GDef.wdEvent.openManghe,this.openManghe);
        this.on(GDef.wdEvent.closeManghe,this.closeManghe);
        this.bindButton(this.btnGiveup,()=>{
            let bolHide = false;
            //闪烁误触
            //审核开关
            let t2 = tyqSDK.getSwitchValue("tyq_wuchu");
            if (!Tool.isInVerify() && t2 > 0 && GD.getLevel() >= t2) {
                if(self.intClickCount >= 1)bolHide = true;
                else {
                    Tool.showBanner();
                    self.intClickCount++;
                    self.scheduleOnce(()=>{
                        Tool.hideBanner();
                    },Math.random() * 0.6 + 1.4);
                }
            }
            else
            {
                bolHide = true;
            }


            if(bolHide)
            {
                Tool.hideBanner();
                self.emit(GDef.wdEvent.levelStart);
                // self.emit(GDef.wdEvent.playEffect,"CoinCollect");
                // self.emit(GDef.wdEvent.addCoin,self.coin,self.comSpineLight.node.getWorldPosition());
                GD.setOnlineTime(new Date().getTime());
                self.node.active = false;
            }
        });
        this.bindButton(this.btnPlay,()=>{
            this.tweenMove.stop();
            TyqEventMgr.ins.sendEvent('观看激励视频-领取金币');
            SdkMgr.ins.showRewardAd("观看激励视频-领取金币", () => {
                TyqEventMgr.ins.sendEvent('观看完激励视频-领取金币');
                Tool.hideBanner();
                this.emit(GDef.wdEvent.playEffect,"CoinCollect");
                this.emit(GDef.wdEvent.levelStart);
                this.emit(GDef.wdEvent.addCoin,this.coin * this.intCoinRatio,this.comSpineLight.node.getWorldPosition());
                GD.setOnlineTime(new Date().getTime());

                this.node.active = false;
            },()=>{
                this.tweenMove.start();
            },()=>{
                this.tweenMove.start();
            })
        });
    }
    init(type: number = 0){
        var self = this;
        this.type = type;
        this.coin = GD.getRoundCoin();
        console.log("getOnlineTime:" + GD.getOnlineTime());
        console.log("getTime:" + (new Date().getTime()));
        console.log("this.coin:" + this.coin);
        this.labelNumSrc.string = Tool.formatNum(this.coin) + "";
        this.comSpineTitle.node.scale = v3(0,0,0);
        this.nodeProgress.scale = v3(0,0,0);
        this.comSpineLight.node.scale = v3(0,0,0);
        this.nodeGan.scale = v3(0,0,0);
        this.nodeBottom.scale = v3(0,0,0);
        if(this.tweenTitle)this.tweenTitle.stop();
        if(this.tweenProgress)this.tweenProgress.stop();
        if(this.tweenLight)this.tweenLight.stop();
        if(this.tweenGan)this.tweenGan.stop();
        if(this.tweenBottom)this.tweenBottom.stop();
        this.tweenTitle = tween(this.comSpineTitle.node).to(1,{scale:v3(1,1,1)},{easing:"elasticOut"});
        this.tweenProgress = tween(this.nodeProgress).delay(0.05).to(0.3,{scale:v3(1,1,1)},{easing:"elasticOut"}).call(()=>{
            if(type == 1)this.nodeProgress.getComponent("preProgress").addProgress(self.node);
            let index = (GD.getLevel() - 1) % 4;
            if(index % 4 != 3 || type != 1)self.start2();
        });
        this.tweenLight = tween(this.comSpineLight.node).delay(0.1).to(1,{scale:v3(1,1,1)},{easing:"elasticOut"});
        this.tweenGan = tween(this.nodeGan).delay(0.2).to(1,{scale:v3(1,1,1)},{easing:"elasticOut"});
        this.btnGiveup.scale = v3(0,0,0);
        this.tweenBottom = tween(this.nodeBottom).delay(0.3).to(0.3,{scale:v3(1,1,1)},{easing:"elasticOut"});
        this.tweenGiveup = tween(this.btnGiveup).delay(0.3).to(0.3,{scale:v3(1,1,1)});




        this.tweenTitle.start();
        this.tweenProgress.start();

        this.intClickCount = 0;
        //欢迎回来
        if(type == 0)
        {
            this.emit(GDef.wdEvent.playEffect,"Victory");
            this.comSpineTitle.setAnimation(0,"welcome",false);
            Tool.hideBanner();
        }
        else if(type == 1)
        {
            this.comSpineTitle.setAnimation(0,"winTitle1",false);
            
        }
        else 
        {
            this.comSpineTitle.setAnimation(0,"failTitle1",false);
            Tool.hideBanner();
        }

        //插屏
        if(type > 0)
        {
            let t = Math.max(0,5 - (new Date().getTime() - GDef.intRewardTime) / 1000);
            this.tweenInterAd = tween(this.node).delay(t).call(()=>{
                SdkMgr.ins.showIntersAd();
            });
            this.tweenInterAd.start();
        }
        //审核开关
        if(Tool.isInVerify())
        {
            this.btnGiveup.getComponent(Sprite).spriteFrame = this.sprBtn;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.width = this.btnPlay.getComponent(UITransformComponent).contentSize.width;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.height = this.btnPlay.getComponent(UITransformComponent).contentSize.height;
        }
        else 
        {
            this.btnGiveup.getComponent(Sprite).spriteFrame = null;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.width = this.btnGiveup.children[0].getComponent(UITransformComponent).contentSize.width;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.height = this.btnGiveup.children[0].getComponent(UITransformComponent).contentSize.height;
        }
    }

    start2(){
        var self = this;

        this.tweenLight.start();
        this.tweenGan.start();
        this.tweenBottom.start();
        this.tweenGiveup.start();

        //发光
        this.comSpineLight.setAnimation(0,"light",true);
        //杆子
        this.labelNum.string = Tool.formatNum(this.coin) + "";
        self.intCoinRatio = 2;
        let sp = 1 / this.speed;
        let sp2 = sp * 2;
        this.tweenMove = tween(this.nodeBone).set({position:v3(-200,-63,0)}).by(sp,{position:v3(66.5,0,0)}).call(()=>{
            self.intCoinRatio = 3;
            self.labelNum.string = (Tool.formatNum(this.coin * self.intCoinRatio)) + "";
            self.sprSprite[0].enabled = false;
            self.sprSprite[1].enabled = true;
        }).by(sp2,{position:v3(133,0,0)}).call(()=>{
            self.intCoinRatio = 4;
            self.labelNum.string = (Tool.formatNum(this.coin * self.intCoinRatio)) + "";
            self.sprSprite[1].enabled = false;
            self.sprSprite[2].enabled = true;
        }).by(sp2,{position:v3(133,0,0)}).call(()=>{
            self.intCoinRatio = 5;
            self.labelNum.string = (Tool.formatNum(this.coin * self.intCoinRatio)) + "";
            self.sprSprite[2].enabled = false;
            self.sprSprite[3].enabled = true;
        }).by(sp,{position:v3(66.5,0,0)}).by(sp,{position:v3(-66.5,0,0)}).call(()=>{
            self.intCoinRatio = 4;
            self.labelNum.string = (Tool.formatNum(this.coin * self.intCoinRatio)) + "";
            self.sprSprite[3].enabled = false;
            self.sprSprite[2].enabled = true;
        }).by(sp2,{position:v3(-133,0,0)}).call(()=>{
            self.intCoinRatio = 3;
            self.labelNum.string = (Tool.formatNum(this.coin * self.intCoinRatio)) + "";
            self.sprSprite[2].enabled = false;
            self.sprSprite[1].enabled = true;
        }).by(sp2,{position:v3(-133,0,0)}).call(()=>{
            self.intCoinRatio = 2;
            self.labelNum.string = (Tool.formatNum(this.coin * self.intCoinRatio)) + "";
            self.sprSprite[1].enabled = false;
            self.sprSprite[0].enabled = true;
        }).by(sp,{position:v3(-66.5,0,0)}).union().repeatForever();
        this.tweenMove.start();
    }
    openManghe(){
    }
    closeManghe(){
        this.start2();
    }
    onDisable(){
        if(this.tweenInterAd)this.tweenInterAd.stop();
        if (GD.getLevel() % 4 == 1)sys.garbageCollect();
        this.tweenMove.stop();
    }
}   
